/******************************************************************************
 Copyright (c) 2022-2025 Qualcomm Technologies International, Ltd.
 All Rights Reserved.
 Qualcomm Technologies International, Ltd. Confidential and Proprietary.

 REVISION:      $Revision: #14 $
******************************************************************************/

#ifndef GATT_XPAN_SERVER_PRIVATE_H
#define GATT_XPAN_SERVER_PRIVATE_H

#include "csr_synergy.h"

#ifndef EXCLUDE_GATT_XPAN_SERVER_MODULE

#include "gatt_xpan_server.h"
#include "csr_util.h"
#include "csr_list.h"
#include "csr_message_queue.h"
#include "gatt_xpan_server_db.h"
#include "csr_bt_gatt_prim.h"
#include "gatt_xpan_server_debug.h"
#include "gatt_xpan_server_handover.h"
#include "csr_bt_gatt_client_util_lib.h"
/*#include "gatt_xpan_server_handover_mgr.h"*/
/* Need to remove this qbl_types header in future when platform team will fix the issue for undefined Bool - ADK-10757*/
#include "qbl_types.h"
#define GATT_XPAN_MAX_CONNECTIONS  (3)

#define XpanMessageSend(TASK, ID, MSG) {\
    MSG->id = ID; \
    CsrSchedMessagePut(TASK, XPAN_SERVER_PRIM, MSG);\
    }

typedef uint8           ConnectionCount;
typedef uint16          TcpPort;
typedef uint16          UdpPort;

/* Current Ssid characteristic struct.
*/
typedef struct
{
    uint8   currentSsidLength;
    uint8   *currentSsid;
    uint8   roles;
    uint8   status;
}Ssid;

/* Client data, This structure contains data for each connected client. */
typedef struct
{
    ConnectionId              cid;
    GattXpanServerConfig     clientCfg;
    TwtConfiguration          twtConfig;
    ClearToSend               clearToSend;
    XpanAudioBearerSwitchResponse audioBearerStatus;
} GattXpanClientData;

/* Connected client linked list structures */
typedef struct XpanClientDataElementTag
{
    struct XpanClientDataElementTag *next;
    struct XpanClientDataElementTag *prev;
    GattXpanClientData              clientData;
} XpanClientDataElement;

/* Overall structure to include all the characteristics.*/
typedef struct
{
    StoredSsid                     storedSsid;
    Ipv4Address                    ipv4Address;
    Ipv6Address                    ipv6Address;
    TcpPort                        l2capTcpPort;
    UdpPort                        udpPort;
    ServiceUuid                    mDnsSrvUuid[16];
    ConnectedDevices               connectedDevices;
    BearerPreference               bearerPreference;
    MacAddress                     macAddress;
    RequestedSapPowerState         requestedSapPowerState;
    XpanServerFeatures             features;
    PresentDevices                 numDevices;
    VoiceBackChannelPeriodicity    periodicity;
    TwtStatus                      twtStatus;
    SapConnectionStatus            sapConnectionStatus;
    CsrCmnList_t                   connectedClients;
} GattXpanData;

/* The XPAN server internal structure. */
typedef struct
{
    AppTaskData             libTask;
    AppTask                 appTask;
    CsrBtGattId             gattId;
    uint16                  serviceHandle;
    ServiceHandle           startHandle;
    uint16                  endHandle;
    GattXpanData            data;
    struct XpanHandoverMgr* handoverMgr;
    uint32                  characteristicsBitMask[GATT_XPAN_CHARACTERISTICS_MASK_SIZE];
} GattXpanServerData;

typedef struct
{
    uint16 size;
    bool   error;
    uint8* dataStart;
    uint8* data;
} GattXpanBuffIterator;

typedef struct
{
    const CsrBtGattAccessInd* accessInd;
    uint8  bufferIndex;
    uint16 offset;
    uint8 error;
} AccessIndIterator;

void xpanServerRespondToCharacteristicRead(CsrBtGattDbAccessReadInd *readInd, uint8 *value, uint16 valueLength, CsrBtGattId gattId);

void xpanServerCpyArrayFromIterator(uint8 *dest, GattAccessWriteIndIterator* src, uint16 len);

void xpanCharacteristicReadResponse8(CsrBtGattDbAccessReadInd *readInd, uint8 data, CsrBtGattId gattId);

void xpanCharacteristicReadResponse16(CsrBtGattDbAccessReadInd *readInd, uint16 data, CsrBtGattId gattId);

void xpanCharacteristicReadResponse32(CsrBtGattDbAccessReadInd *readInd, uint32 data, CsrBtGattId gattId);

Bool xpanFindCid(CsrCmnListElm_t *elem, void *value);

bool xpanValidateIteratorBufferUsage(GattAccessWriteIndIterator *iter);

uint16 xpanGetIteratorBufferRemainingSize(GattAccessWriteIndIterator *iter);

XpanClientDataElement *xpanFindClient(CsrCmnList_t *connectedClients, ConnectionId cid);

#define XPAN_ADD_CLIENT(_List) \
    (XpanClientDataElement *)CsrCmnListElementAddLast(&(_List), \
                                                        sizeof(XpanClientDataElement))

#define XPAN_FIND_CID(_List,_cid) \
    ((XpanClientDataElement *)CsrCmnListSearch(&(_List), \
                                                        xpanFindCid, \
                                                        (void *)(_cid)))

#define XPAN_REMOVE_CLIENT(_List,_Elem) \
    (CsrCmnListElementRemove((CsrCmnList_t *)&(_List), \
                             (CsrCmnListElm_t *)(_Elem)))

#define MAKE_XPAN_MESSAGE(TYPE) \
    TYPE * message = (TYPE *)CsrPmemZalloc(sizeof(TYPE))


/***************************************************************************
NAME
    xpanServerHandleChangeStoredSsid

DESCRIPTION
    Handles change of Stored Ssid characteristic by notifying subscribed clients.
*/
void xpanServerHandleChangeStoredSsid(GattXpanServerData *gattXpanServerInst);

/***************************************************************************
NAME
    xpanServerHandleChangeL2capTcpPort

DESCRIPTION
    Handles change of L2cap Tcp Port characteristic by notifying subscribed clients.
*/
void xpanServerHandleChangeL2capTcpPort(GattXpanServerData *inst);

/***************************************************************************
NAME
    xpanServerHandleChangeUdpPort

DESCRIPTION
    Handles change of Udp port characteristic by notifying subscribed clients.
*/
void xpanServerHandleChangeUdpPort(GattXpanServerData *inst);

/***************************************************************************
NAME
    xpanServerHandleChangeTwtConfiguration

DESCRIPTION
    Handles change of Twt Configuration characteristic by notifying specified client.
*/
void xpanServerHandleChangeTwtConfiguration(GattXpanServerData *inst, ConnectionId cid);

/***************************************************************************
NAME
    xpanServerHandleChangeClearToSend

DESCRIPTION
    Handles change of Clear to send characteristic by notifying subscribed specified client.
*/
void xpanServerHandleChangeClearToSend(GattXpanServerData *inst, ConnectionId cid);

/***************************************************************************
NAME
    xpanServerHandleChangeIpv4Address

DESCRIPTION
    Handles change of Ipv4 Address characteristic by notifying subscribed clients.
*/
void xpanServerHandleChangeIpv4Address(GattXpanServerData *inst);

/***************************************************************************
NAME
    xpanServerHandleChangeIpv6Address

DESCRIPTION
    Handles change of Ipv6 address characteristic by notifying subscribed clients.
*/
void xpanServerHandleChangeIpv6Address(GattXpanServerData *inst);

XpanClientDataElement * xpanAddClient(CsrCmnList_t *connectedClients, ConnectionId cid);

#endif /* EXCLUDE_GATT_XPAN_SERVER_MODULE */

#endif /* GATT_XPAN_SERVER_PRIVATE_H */
